package org.jeecg.modules.website.controller.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.website.entity.WebWebsiteMenu;
import org.jeecg.modules.website.service.IWebWebsiteMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
* @Description: web_website_menu
* @Author: jeecg-boot
* @Date:   2024-07-12
* @Version: V1.0
*/
@Api(tags="网站API-一级菜单资源接口")
@RestController
@RequestMapping("/api/web/webWebsiteMenu")
@Slf4j
public class WebWebsiteMenuApiController extends JeecgController<WebWebsiteMenu, IWebWebsiteMenuService> {
   @Autowired
   private IWebWebsiteMenuService webWebsiteMenuService;
    @Autowired
   private ISysDictService sysDictService;
    @Autowired
    private ISysDictItemService sysDictItemService;

   /**
    * 分页列表查询
    *
    * @param webWebsiteMenu
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   //@AutoLog(value = "web_website_menu-分页列表查询")
   @ApiOperation(value="web_website_menu-分页列表查询", notes="web_website_menu-分页列表查询")
   @GetMapping(value = "/list")
   public Result<IPage<WebWebsiteMenu>> queryPageList(WebWebsiteMenu webWebsiteMenu,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<WebWebsiteMenu> queryWrapper = QueryGenerator.initQueryWrapper(webWebsiteMenu, req.getParameterMap());
       Page<WebWebsiteMenu> page = new Page<WebWebsiteMenu>(pageNo, pageSize);
       queryWrapper.eq("publish_status",1); // 只查询已发布的
       queryWrapper.last(" order by order_num asc"); // 根据发布时间排序
       IPage<WebWebsiteMenu> pageList = webWebsiteMenuService.page(page, queryWrapper);
       pageList.getRecords().forEach(e ->{
           if(!e.getMenuCode().equals("0")){
               e.setDictModelList(sysDictService.getDictItems(e.getMenuCode()));
           }
           e.setMenuName(sysDictService.queryDictTextByKey("web_website_menu",e.getMenuName()));
       });

       return Result.OK(pageList);
   }



   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   //@AutoLog(value = "web_website_menu-通过id查询")
   @ApiOperation(value="web_website_menu-通过id查询", notes="web_website_menu-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<WebWebsiteMenu> queryById(@RequestParam(name="id",required=true) String id) {
       WebWebsiteMenu webWebsiteMenu = webWebsiteMenuService.getById(id);
       if(webWebsiteMenu==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(webWebsiteMenu);
   }


}
